package com.wk.net.heartbeat.source;

import java.io.IOException;
import java.io.OutputStream;

import com.wk.net.heartbeat.HeartbeatTransferer;



/**
 * 任务类：发送心跳信号
 * 
 * @author Bai Yu
 *
 */
class SendHeartbeat<HeartbeatType> implements Runnable {
	public SendHeartbeat(HeartbeatMaker<HeartbeatType> maker, HeartbeatTransferer<HeartbeatType> transferer, HeartbeatSender sender) {
		this.maker = maker;
		this.transferer = transferer;
		this.sender = sender;
	}

	@Override
	public void run() {
		HeartbeatType heartbeat = maker.make();
		if (heartbeat != null) {
			OutputStream output;
			try {
				output = sender.getOutputStream();
			} catch (IOException e) {
				return;
			}
			try {
				transferer.send(heartbeat, output);
			} catch (IOException e) {
			} finally {
				try {
					output.close();
				} catch (IOException e) {
				}
			}
		}
	}

	private HeartbeatMaker<HeartbeatType> maker;
	private HeartbeatTransferer<HeartbeatType> transferer;
	private HeartbeatSender sender;
}
